package cn.iocoder.yudao.module.vote.dal.mysql.voteinfo;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteinfo.VoteInfoDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserinfo.VoteUserInfoDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.vote.controller.admin.voteinfo.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 投票规则 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface VoteInfoMapper extends BaseMapperX<VoteInfoDO> {

    default PageResult<VoteInfoDO> selectPage(VoteInfoPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VoteInfoDO>()
                .likeIfPresent(VoteInfoDO::getVoteName, reqVO.getVoteName())
                .betweenIfPresent(VoteInfoDO::getBeginTime, reqVO.getBeginBeginTime(), reqVO.getEndBeginTime())
                .betweenIfPresent(VoteInfoDO::getEndTime, reqVO.getBeginEndTime(), reqVO.getEndEndTime())
                .eqIfPresent(VoteInfoDO::getVoteCount, reqVO.getVoteCount())
                .eqIfPresent(VoteInfoDO::getLimitCount, reqVO.getLimitCount())
                .eqIfPresent(VoteInfoDO::getRemark, reqVO.getRemark())
                .eqIfPresent(VoteInfoDO::getImportCount, reqVO.getImportCount())
                .eqIfPresent(VoteInfoDO::getIsPush, reqVO.getIsPush())
                .betweenIfPresent(VoteInfoDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .eqIfPresent(VoteInfoDO::getVoteType, reqVO.getVoteType())
                .notIn(CollUtil.isNotEmpty(reqVO.getNoInVoteType()),VoteInfoDO::getVoteType,reqVO.getNoInVoteType())
                .orderByDesc(VoteInfoDO::getId));
    }

    default List<VoteInfoDO> selectList(VoteInfoExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoteInfoDO>()
                .likeIfPresent(VoteInfoDO::getVoteName, reqVO.getVoteName())
                .betweenIfPresent(VoteInfoDO::getBeginTime, reqVO.getBeginBeginTime(), reqVO.getEndBeginTime())
                .betweenIfPresent(VoteInfoDO::getEndTime, reqVO.getBeginEndTime(), reqVO.getEndEndTime())
                .eqIfPresent(VoteInfoDO::getVoteCount, reqVO.getVoteCount())
                .eqIfPresent(VoteInfoDO::getLimitCount, reqVO.getLimitCount())
                .eqIfPresent(VoteInfoDO::getIsPush, reqVO.getIsPush())
                .eqIfPresent(VoteInfoDO::getRemark, reqVO.getRemark())
                .eqIfPresent(VoteInfoDO::getImportCount, reqVO.getImportCount())
                .betweenIfPresent(VoteInfoDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(VoteInfoDO::getId));
    }


    /**
     * 获取候选人选举最近一条记录
     * @return
     */
    VoteInfoDO getCurrVote(@Param("voteType")Integer voteType,@Param("tenantId")Long tenantId);

    default List<VoteInfoDO> getByVoteType(Integer voteType){
        return selectList(new LambdaQueryWrapperX<VoteInfoDO>()
                .eqIfPresent(VoteInfoDO::getVoteType, voteType)
                .orderByDesc(VoteInfoDO::getId));
    }

    /**
     * 更新投票人数
     * @param voteId
     * @return
     */
    int updateVoteCount(Integer voteId);


}
